#include "shadow.h"
//#include <iostream>

Shadow::Shadow(int w,int h)
{
    glGenTextures(1, &_shadowTexture);
    glBindTexture(GL_TEXTURE_2D, _shadowTexture);

    glTexImage2D(GL_TEXTURE_2D, 0, GL_DEPTH_COMPONENT,w,h, 0, GL_DEPTH_COMPONENT,GL_FLOAT,0);

    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);

    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_R, GL_CLAMP);

    glBindTexture(GL_TEXTURE_2D,0);
}

GLuint Shadow::getID()
{
    return _shadowTexture;
}

void Shadow::displayShadowMapTexture(GLuint fbo_shad_id)
{
//    GLint textureWidth=512, textureHeight=512;
   // float* textureBuf;
   // glBindFramebuffer(GL_FRAMEBUFFER, fbo_shad_id);
   // glDrawBuffer(GL_NONE);
    //glReadBuffer(GL_NONE);

    //textureBuf = new float [textureWidth * textureHeight];
    //glReadPixels(0,0,textureWidth,textureHeight,GL_DEPTH_COMPONENT,GL_FLOAT,textureBuf);


  //  for (int i=0;i< textureWidth*textureHeight;i++)
    //  std::cout<<textureBuf[i]<<std::endl;

    //glBindFramebuffer(GL_FRAMEBUFFER, 0);
    // create new texture
    //GLuint texid;
    //glGenTextures(1,&texid);

    // bind texture
    glBindTexture(GL_TEXTURE_2D,_shadowTexture);

    // set texture parameters
/*
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_R, GL_CLAMP);

    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);

    // allocate memory and load data
    glTexImage2D(GL_TEXTURE_2D, 0, GL_DEPTH_COMPONENT, textureWidth, textureHeight, 0,
                 GL_DEPTH_COMPONENT, GL_FLOAT, textureBuf);
*/
    glBegin(GL_QUADS);
    glTexCoord2d(0.0, 0.0);
    glVertex3d(0.0, 2.0, 0.0);  // Bottom Left Of The Texture and Quad
    glTexCoord2d(1.0, 0.0);
    glVertex3d( 0.0, 5.0,0.0);  // Bottom Right Of The Texture and Quad
    glTexCoord2d(1.0, 1.0);
    glVertex3d( 0.0, 5.0,3.0);  // Top Right Of The Texture and Quad
    glTexCoord2d(0.0, 1.0);
    glVertex3d(0.0,  2.0,3.0);  // Top Left Of The Texture and Quad
    glEnd();
    glBindTexture(GL_TEXTURE_2D,0);

//    glDeleteTextures(1,&texid);
}

Shadow::~Shadow()
{
    glDeleteTextures(1,&_shadowTexture);
}
